package com.ruoyi.system.service.impl;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SnUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WeChatUtils;
import com.ruoyi.common.utils.property.WeChatProperty;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.PayService;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class PayServiceImpl implements PayService {

    private static String  appUrl = SpringUtils.getBean(WeChatProperty.class).getAppUrl();

    @Autowired
    private AOrderMapper aOrderMapper;

    @Autowired
    private AHousingMapper aHousingMapper;

    @Autowired
    private AVipRentMapper aVipRentMapper;

    @Autowired
    private AVipTransMapper aVipTransMapper;

    @Autowired
    private AVipTransSubMapper aVipTransSubMapper;

    @Autowired
    private AH5vipTransMapper ah5vipTransMapper;

    @Autowired
    private AH5vipTransSubMapper ah5vipTransSubMapper;
    @Autowired
    private AUserMapper aUserMapper;

    @Autowired
    private AVipUserMapper aVipUserMapper;

    @Override
    public WxPayMpOrderResult walletPay(AOrder aOrder) {
        aOrder.setUserId(LoginUtils.getUser().getId());
        aOrder.setCreateTime(DateUtils.getNowDate());
        String no = SnUtils.orderNo();
        aOrder.setOrderNo(no);
        String str = "转让发布";
        BigDecimal total = new BigDecimal("0.00");
        BigDecimal topTotal = new BigDecimal("0.00");
        if(aOrder.getIsType().equals("1")){
            AVipTrans aVipTrans = aVipTransMapper.selectAVipTransById(aOrder.getVipId());
            total = BigDecimal.valueOf(aVipTrans.getPrice());
            aOrder.setOrPrice(total);

            if(aOrder.getTopId() != null){
                AVipTransSub aVipTransSub = aVipTransSubMapper.selectAVipTransSubById(aOrder.getTopId());
                topTotal = aVipTransSub.getPrice();
                aOrder.setTopMoney(topTotal);
            }

            total = total.add(topTotal);
            aOrder.setTotalMoney(total);
        }
        if(aOrder.getIsType().equals("2")){
            AVipRent aVipRent = aVipRentMapper.selectAVipRentById(aOrder.getVipId());
            total = aVipRent.getPrice();
            aOrder.setOrPrice(total);
            aOrder.setTotalMoney(total);
            str = "出租发布";
        }
        aOrder.setTypeName(str);
        aOrderMapper.insertAOrder(aOrder);

        WxPayMpOrderResult result = WeChatUtils.weChatPay_mini(str, no, LoginUtils.getUser().getOpenId(), total, appUrl + "/xcx/pay/AOrderPayCallBack");
        return result;
    }

    @Override
    public void wechatPayCallBack(String orderNo, String wechatNo) {
        AOrder order = aOrderMapper.selectAOrderByOrderNo(orderNo);
        order.setPayNo(wechatNo);//支付单号
        order.setPayStatus(2L);//支付完成
        order.setPayTime(DateUtils.getNowDate());
        aOrderMapper.updateAOrder(order);
        AHousing housing = aHousingMapper.selectAHousingById(order.getHousId());
        if(housing != null){
            housing.setIsPay("0");
            housing.setIsTop("0");
            if(order.getIsType().equals("1")){
                AVipTrans aVipTrans = aVipTransMapper.selectAVipTransById(order.getVipId());
                if(order.getVipId().equals(3)){
                    Date date = getDate(aVipTrans.getTransDay()== null ? 0 :aVipTrans.getTransDay());
                    housing.setTopTime(date);
                }else{
                    AVipTransSub aVipTransSub = aVipTransSubMapper.selectAVipTransSubById(order.getTopId());
                    Date date = getDate(aVipTransSub.getDes()== null ? 0 :aVipTransSub.getDes());
                    housing.setTopTime(date);
                }
                housing.setTopLevel(aVipTrans.getTopLevel());
            }
            if(order.getIsType().equals("2")){
                AVipRent aVipRent = aVipRentMapper.selectAVipRentById(order.getVipId());
                Date date = getDate(aVipRent.getTopDay() == null ? 0 : aVipRent.getTopDay());
                housing.setTopTime(date);
                housing.setTopLevel(aVipRent.getTopLevel());
            }
            aHousingMapper.updateAHousing(housing);
        }
    }

    @Override
    public WxPayMpOrderResult walletPayH(AOrder aOrder) {
        aOrder.setUserId(LoginUtils.getUser().getId());
        aOrder.setCreateTime(DateUtils.getNowDate());
        String no = SnUtils.orderNo();
        aOrder.setOrderNo(no);
        String str = "H5转让发布";
        BigDecimal total = new BigDecimal("0.00");

        AH5vipTrans tr = ah5vipTransMapper.selectAH5vipTransById(aOrder.getVipId());
        total = tr.getPrice();
        aOrder.setOrPrice(total);
        if(aOrder.getTopId() != null){
            AH5vipTransSub sub = ah5vipTransSubMapper.selectAH5vipTransSubById(aOrder.getTopId());
            total = total.add(sub.getPrice());
            aOrder.setTopMoney(sub.getPrice());
        }

        aOrder.setTotalMoney(total);

        if(aOrder.getIsType().equals("4")){
            str = "H5出租发布";
        }
        aOrder.setTypeName(str);
        aOrderMapper.insertAOrder(aOrder);
//        if(aOrder.getPayType().equals("2")){
//
//        }else {
            WxPayMpOrderResult result = WeChatUtils.weChatPay_h(str, no, LoginUtils.getUser().getMiniOpenId(), total, appUrl + "xcx/pay/AOrderPayCallBackH");
//        }
        return result;
    }

    @Override
    public void wechatPayCallBackH(String orderNo, String wechatNo) {
        AOrder order = aOrderMapper.selectAOrderByOrderNo(orderNo);
        order.setPayNo(wechatNo);//支付单号
        order.setPayStatus(2L);//支付完成
        order.setPayTime(DateUtils.getNowDate());
        aOrderMapper.updateAOrder(order);
        AHousing housing = aHousingMapper.selectAHousingById(order.getHousId());
        if(housing != null){
            housing.setIsPay("0");
            housing.setPayTime(DateUtils.getNowDate());

            if(order.getTopId() != null){
                AH5vipTransSub sub = ah5vipTransSubMapper.selectAH5vipTransSubById(order.getTopId());
                Date date = getDate(sub.getDes() == null ? 0 :sub.getDes());
                housing.setTopTime(date);
                housing.setIsTop("0");
            }
            housing.setTopLevel("0");
            aHousingMapper.updateAHousing(housing);
        }
    }

    @Override
    public String walletPayNa(AOrder aOrder) {
        aOrder.setUserId(LoginUtils.getUser().getId());
        aOrder.setCreateTime(DateUtils.getNowDate());
        String no = SnUtils.orderNo();
        aOrder.setOrderNo(no);
        String str = "转让发布";
        BigDecimal total = new BigDecimal("0.00");
        BigDecimal topTotal = new BigDecimal("0.00");
        if(aOrder.getIsType().equals("1")){
            AVipTrans aVipTrans = aVipTransMapper.selectAVipTransById(aOrder.getVipId());
            total = BigDecimal.valueOf(aVipTrans.getPrice());
            aOrder.setOrPrice(total);

            if(aOrder.getTopId() != null){
                AVipTransSub aVipTransSub = aVipTransSubMapper.selectAVipTransSubById(aOrder.getTopId());
                topTotal = aVipTransSub.getPrice();
                aOrder.setTopMoney(topTotal);
            }

            total = total.add(topTotal);
            aOrder.setTotalMoney(total);
        }
        if(aOrder.getIsType().equals("2")){
            AVipRent aVipRent = aVipRentMapper.selectAVipRentById(aOrder.getVipId());
            total = aVipRent.getPrice();
            aOrder.setOrPrice(total);
            aOrder.setTotalMoney(total);
            str = "出租发布";
        }
        aOrder.setTypeName(str);
        aOrderMapper.insertAOrder(aOrder);

        String result = WeChatUtils.weChatPay_na(str, no, total, appUrl + "xcx/pay/AOrderPayNaCallBack");
        return result;
    }

    @Override
    public void wechatPayCallBackNa(String orderNo, String wechatNo) {
        AOrder order = aOrderMapper.selectAOrderByOrderNo(orderNo);
        order.setPayNo(wechatNo);//支付单号
        order.setPayStatus(2L);//支付完成
        order.setPayTime(DateUtils.getNowDate());
        aOrderMapper.updateAOrder(order);
        AHousing housing = aHousingMapper.selectAHousingById(order.getHousId());
        if(housing != null){
            housing.setIsPay("0");
            housing.setPayTime(DateUtils.getNowDate());

            if(order.getTopId() != null){
                AH5vipTransSub sub = ah5vipTransSubMapper.selectAH5vipTransSubById(order.getTopId());
                Date date = getDate(sub.getDes() == null ? 0 :sub.getDes());
                housing.setTopTime(date);
                housing.setIsTop("0");
            }
            housing.setTopLevel("0");
            aHousingMapper.updateAHousing(housing);
        }
    }

    @Override
    public WxPayMpOrderResult walletPayUser(AOrder aOrder) {
        String str = "用户信息";
        BigDecimal total = new BigDecimal("0.00");
        String no = SnUtils.orderNo();
        aOrder.setUserId(LoginUtils.getUser().getId());
        aOrder.setCreateTime(DateUtils.getNowDate());
        aOrder.setOrderNo(no);
        aOrder.setTypeName(str);
        AVipUser aVipUser = aVipUserMapper.selectAVipUserById(aOrder.getVipId());
        aOrder.setOrPrice(aVipUser.getVipPrice());
        aOrder.setTotalMoney(aVipUser.getVipPrice());
        total = aVipUser.getVipPrice();
        aOrderMapper.insertAOrder(aOrder);

        WxPayMpOrderResult result = WeChatUtils.weChatPay_mini(str, no, LoginUtils.getUser().getOpenId(), total, appUrl + "xcx/pay/AOrderPayUserCallBack");
        return result;
    }

    @Override
    public void wechatPayCallBackUser(String orderNo, String wechatNo) {
        AOrder order = aOrderMapper.selectAOrderByOrderNo(orderNo);
        order.setPayNo(wechatNo);//支付单号
        order.setPayStatus(2L);//支付完成
        order.setPayTime(DateUtils.getNowDate());
        aOrderMapper.updateAOrder(order);
        AUser aUser = aUserMapper.selectAUserById(order.getUserId());
        if(order.getIsType().equals("6")){
            if(aUser.getIsVip().equals("1")){
                aUser.setIsVip("0");
            }
        }
        if(order.getIsType().equals("7")){
            aUser.setIsVip("2");
        }
        aUserMapper.updateAUser(aUser);
    }

    private Date getDate(Long num) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime plusThirtyDays = now.plusDays(num);
       return Date.from(plusThirtyDays.atZone(ZoneId.systemDefault()).toInstant());
    }

}
